package xin.de.weiauth.service.impl;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.de.weiauth.config.WechatAccountConfig;
import xin.de.weiauth.service.PushMessageService;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class PushMessageServiceImpl implements PushMessageService{
    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private WechatAccountConfig wechatAccountConfig;
    /**
     * 发送定时消息
     * @param openId
     * @param date
     * @param message
     * @return
     */
    public boolean pushQuartzMessage(String openId, String date, String message) {
        WxMpTemplateMessage templateMessage=new WxMpTemplateMessage();
        templateMessage.setTemplateId(wechatAccountConfig.getTemplateId().get("orderStatus"));
        templateMessage.setToUser(openId);
        List<WxMpTemplateData> data= Arrays.asList(
                new WxMpTemplateData("first","提醒"),
                new WxMpTemplateData("event",message),
                new WxMpTemplateData("date",date)
        );
        templateMessage.setData(data);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
           return false;
        }
        return true;
    }
}
